package ast_generator;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * 
 * @author Chu
 *Class contains a static object that will generate a list of ASTTree (1 per token) from a filename.
 */
public class TreeGenerator {

static int size=10000;
static ArrayList[] treeArray= new ArrayList[size]; //arraylist used for debugging. Used to create an arraylist version of the tree
static ArrayList<String> epsilon= new ArrayList<String>();
static ArrayList<ASTTree> tree;

/**
 * This method actually generates the list of ASTTrees.
 * @param s file name we are reading the lexical spec from and creating the tree
 * @return ArrayList of ASTTrees we will use to make the NFA
 */
public static ArrayList<ASTTree> makeTree(String s)
{
//initialize variables
for( int i=0; i<size; i++ ) treeArray[i]= new ArrayList<String>();
epsilon.add("epsilon");
LexScanner scanner=new LexScanner();
tree=new ArrayList<ASTTree>();
scanner.generateCharacterClasses(s);
HashMap<String,ArrayList<String>> characterClasses=scanner.getCharacterClasses();
HashMap<String,String> tokenMap=scanner.getTokenMap();
Object[] regex= tokenMap.keySet().toArray();

//iterate through tokenMap to generate ArrayList of AST Trees. 1 for each token type
int i=0;
while(i<regex.length)
{
	//System.out.println("REGEX: "+tokenMap.get(regex[i])+ " TOKEN NAME: "+regex[i]);
	RecursiveDescentParser Parser=new RecursiveDescentParser(tokenMap.get(regex[i]),characterClasses);
	if(i==4)
	{
	System.out.println("");	
	}
	ASTNode n=Parser.parse();
	
	
	tree.add(new ASTTree(n,(String)regex[i]));
	/*
	convertToArrayList(1,n);
	System.out.println(treeArray);
	System.out.println(); 
	 */
	i++;
}

//System.out.println("done");
return tree;
}
/**
 * This method actually generates the list of ASTTrees from an arraylist string input representing
 * a single lex spec
 * @param ArrayList in the format {<ID>,<Lex Spec>}
 * @return ArrayList of ASTTrees we will use to make the NFA
 */
public static ArrayList<ASTTree> makeTreeFromString(ArrayList<String> input)
{
//initialize variables
for( int i=0; i<size; i++ ) treeArray[i]= new ArrayList<String>();
epsilon.add("epsilon");
tree=new ArrayList<ASTTree>();
HashMap<String,ArrayList<String>> characterClasses=new HashMap<String, ArrayList<String>>();


	//System.out.println("REGEX: "+tokenMap.get(regex[i])+ " TOKEN NAME: "+regex[i]);
	RecursiveDescentParser Parser=new RecursiveDescentParser(input.get(1),characterClasses);
	ASTNode n=Parser.parse();
	
	tree.add(new ASTTree(n,input.get(0)));
	


//System.out.println("done");
return tree;
}

//used for debugging.
//note this function creates an ArrayList representation of the tree. Also prints the preorder traversal of the tree
private static void convertToArrayList(int i,ASTNode n) 
{
	if(n!=null)
	{
	
	
	if(n.getLeft()!=null)
	{
	convertToArrayList(2*i,n.getLeft());
	}
	 if(n.getValue()!=null)
		{
		System.out.print(n.getValue());
		}
	  else
	  {
		   	System.out.print(epsilon);
	  }
	
   if(n.getRight()!=null)
	{
	convertToArrayList(2*i+1,n.getRight());
	}
   if(n.getValue()!=null)
	{
	treeArray[i-1]=n.getValue();
	}
 else
 {
	   	   treeArray[i-1]=epsilon;
 }
   
  
	}
}
}
